package hlhj.fhp.tvlib.activitys

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.widget.Toast
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.google.gson.Gson
import com.liaoinstan.springview.container.DefaultFooter
import com.liaoinstan.springview.container.DefaultHeader
import com.liaoinstan.springview.widget.SpringView
import com.lzy.okgo.OkGo
import com.lzy.okgo.callback.StringCallback
import com.lzy.okgo.model.Response
import com.lzy.okgo.request.base.Request
import hlhj.fhp.tvlib.Contents
import hlhj.fhp.tvlib.R
import hlhj.fhp.tvlib.customView.LoadingDialog
import hlhj.fhp.tvlib.javabean.LiveListBean
import kotlinx.android.synthetic.main.activity_live_list_aty.*
import java.util.ArrayList

class LiveListAty : BaseAty() {
    private var page=1
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var adp :BaseQuickAdapter<LiveListBean.DataBean,BaseViewHolder>
    private val datas=ArrayList<LiveListBean.DataBean>()
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        supportActionBar!!.hide()

        loadingDialog= LoadingDialog(this)
        initRcy()
        supportActionBar!!.hide()
        setSp()

        btExit.setOnClickListener {
            finish()
        }
    }

    override fun getContentId(): Int {
        return R.layout.activity_live_list_aty
    }

    private fun initRcy() {
        adp=object :BaseQuickAdapter<LiveListBean.DataBean,BaseViewHolder>(R.layout.live_item,datas){
            override fun convert(helper: BaseViewHolder?, item: LiveListBean.DataBean?) {
                Glide.with(mContext).load(Contents.IMG_URL+item?.live_thumb).into(helper!!.getView(R.id.itemImg))
                when (item?.live_status){
                    1->{
                        if (item?.live_type==1){
                            helper?.setImageResource(R.id.imgType,R.drawable.ic_zb_zhengzaizhibo)
                        }else{
                            helper?.setImageResource(R.id.imgType,R.drawable.ic_zb_huifang)
                        }
                    }
                    2->{
                        if (item?.live_type==1){
                            helper?.setImageResource(R.id.imgType,R.drawable.ic_zb_tuwenzhibo)
                        }else{

                        }
                    }
                }
                helper?.setText(R.id.itemTittle,item?.live_title)

            }
        }
        recyclerview.layoutManager=LinearLayoutManager(this,LinearLayoutManager.VERTICAL,false)
        recyclerview.adapter=adp

        adp.setOnItemClickListener { adapter, view, position ->
            if (datas[position].live_status==1){
                val intent = Intent(this@LiveListAty, LiveAty::class.java)
                intent.putExtra("id",datas[position].id)
                intent.putExtra("tittle",datas[position].live_title)
                intent.putExtra("url",datas[position].live_source)
                startActivity(intent)
            }else{
                val intent = Intent(this@LiveListAty, TextLiveAty::class.java)
                intent.putExtra("id",datas[position].id)
                intent.putExtra("thumb",datas[position].live_thumb)
                intent.putExtra("tittle",datas[position].live_title)
                startActivity(intent)
            }
        }

        getData()
    }

    private fun setSp() {
        spView.header=DefaultHeader(this)
        spView.footer=DefaultFooter(this)
        spView.setListener(object :SpringView.OnFreshListener{
            override fun onLoadmore() {
                page++
                getData()
            }

            override fun onRefresh() {
                page=1
                datas.clear()
                getData()
            }
        })
    }

    private fun getData() {
        OkGo.get<String>(Contents.BaseUrl+Contents.LIVE_LIST)
                .params("page",page)
                .execute(object :StringCallback(){
                    override fun onStart(request: Request<String, out Request<Any, Request<*, *>>>?) {
                        super.onStart(request)
                        loadingDialog.show()
                    }
                    override fun onSuccess(response: Response<String>?) {
                        loadingDialog.dismiss()
                        spView.onFinishFreshAndLoad()
                        val str = response?.body()
                        val listBean = Gson().fromJson(str, LiveListBean::class.java)
                        if (listBean.code==1){
                            datas.addAll(listBean.data)
                            adp.notifyDataSetChanged()
                        }else{
                            Toast.makeText(this@LiveListAty,listBean.msg,Toast.LENGTH_SHORT).show()
                        }
                    }
                })
    }
}
